﻿using System;
using System.Collections.Generic;
#if UNITY_IOS && !UNITY_EDITOR
using UnityEngine.iOS;
using Unity.Notifications.iOS;
#endif
/// <summary>
/// IOS本地推送脚本
/// </summary>
public class IOSNotificationMgr : INotification
{
#if UNITY_IOS && !UNITY_EDITOR
    int identifierIndex = 0;

    string identifierTag = "Identifier";
    public static Dictionary<string, iOSNotification> startUpNotificationDic = new Dictionary<string, iOSNotification>();
#endif
    public void ApplyNotificationCenter()
    {
#if UNITY_IOS && !UNITY_EDITOR
        identifierIndex = 0;
#endif
    }

    public void SendNotification(string title, string des, string iconName, float delayTime)
    {
#if UNITY_IOS && !UNITY_EDITOR
        iOSNotificationTimeIntervalTrigger timeTrigger = new iOSNotificationTimeIntervalTrigger()
        {
            TimeInterval = new TimeSpan(0, 0, (int)delayTime),
            Repeats = false
        };
        identifierIndex++;
        iOSNotification notification = new iOSNotification()
        {
            // You can optionally specify a custom identifier which can later be 
            // used to cancel the notification, if you don't set one, a unique 
            // string will be generated automatically.
            Identifier = $"{identifierTag}{identifierIndex}",
            Title = title,
            Body = $"{des}",
            Subtitle = "",
            ShowInForeground = true,
            ForegroundPresentationOption = (PresentationOption.Alert | PresentationOption.Sound),
            CategoryIdentifier = "AppcpiNotification",
            ThreadIdentifier = "thread1",
            Trigger = timeTrigger,
        };
        iOSNotificationCenter.ScheduleNotification(notification);
        startUpNotificationDic.Add(notification.Identifier, notification);
#endif
    }

    public void StopAllNotification()
    {
#if UNITY_IOS && !UNITY_EDITOR
        foreach (string identifier in startUpNotificationDic.Keys)
        {
            iOSNotificationCenter.RemoveDeliveredNotification(identifier);
            iOSNotificationCenter.RemoveScheduledNotification(identifier);
        }
        startUpNotificationDic.Clear();
#endif
    }
}
